package com.loby.zk.util;

import com.loby.data.base.pojo.RtMonStationPollutant;
import com.loby.data.base.pojo.TMonForwardInfos;
import com.loby.data.base.pojo.TMonMonitorStation;
import com.loby.data.history.pojo.MHisDataTables;

import com.loby.monitor.client.propertie.ClientProperty;
import com.loby.monitor.client.propertie.ForwardInfo;
import com.loby.monitor.excess.local.*;
import lombok.extern.log4j.Log4j;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Log4j
public class PropertyUtil {



    public static ClientProperty getProperty(TMonMonitorStation monitorStation, List<MHisDataTables> tables, List<TMonForwardInfos> infos){


        ClientProperty property = new ClientProperty();
        property.setClientCode(monitorStation.getStationUniqueCode());
        property.setName(monitorStation.getStationName());
        property.setProtocol((int) monitorStation.getCommProtocol());
        property.setPwd(monitorStation.getRemotePwd());
        property.setRequireResponse(monitorStation.getCommProtocolIsResponse() == 1);
        property.setRequireSplit(false);
        List<ForwardInfo> forwardInfos  =new ArrayList<>(1);
        infos.stream().forEach(s-> forwardInfos.add(ForwardInfo.builder().ip(s.getIp()).enabled(s.isEnable()).port((int) s.getPort()).build()));
        property.setForwardInfos(forwardInfos);
        for (MHisDataTables table : tables) {
            switch (table.getTableType()){
                case tt0:
                    property.setRealTableName(table.getTableName());
                    break;
                case tt1:
                    property.setMinTableName(table.getTableName());
                    break;
                case tt2:
                    property.setHourTableName(table.getTableName());
                    break;
                case tt3:
                    property.setDayTableName(table.getTableName());
                    break;
                default:break;
            }
        }

        return property;
    }

    public static ClientProperty addCondition(ClientProperty clientProperty,List<RtMonStationPollutant> pollutants){

        try{

            Map<String, DataExcessCondition> conditionMap = new HashMap<>(pollutants.size());
            boolean isExcessEnable = false,isOverRangeEnable = false;
            for (RtMonStationPollutant p : pollutants) {
                boolean excessEnable = p.getOverStandardLower()!=null && p.getOverStandardUpper()!=null;
                isExcessEnable = isExcessEnable | excessEnable;
                isOverRangeEnable = isOverRangeEnable | p.isRangeAlarm();
                conditionMap.put(p.getFkPollutantCode(), DataExcessCondition.builder().dataType(InstrumentDataType.AI).compositionCode(p.getFkPollutantCode())
                        .rangeUpperLimit(p.getRangeUpper()).rangeLowerLimit(p.getRangeLower()).overRangeEnabled(p.isRangeAlarm())
                        .upperThreshold(p.getOverStandardUpper()).lowerThreshold(p.getOverStandardLower()).excessEnabled(excessEnable)
                        .realDataExcessMode(ExcessMode.valueOf((int) p.getFkDitRealDataAlarmModeNo())).realDataAlarmInterval((int) p.getRealAlarmInterval()).realDataAlarmTimeMode(AlarmTimeMode.valueOf((int) p.getFkDitRealDataAlarmTimeModeNo()))
                        .realZeroValueAlarmAmount((int) p.getRealDataZerosAlarmAmount()).realSameValueAlarmAmount((int) p.getRealDataConstantAlarmAmount())
                        .minDataExcessMode(ExcessMode.valueOf((int) p.getFkDitMinDataAlarmModeNo())).minDataAlarmInterval((int) p.getMinAlarmInterval()).minDataAlarmTimeMode(AlarmTimeMode.valueOf((int) p.getFkDitMinDataAlarmTimeModeNo()))
                        .minZeroValueAlarmAmount((int) p.getMinDataZerosAlarmAmount()).minSameValueAlarmAmount((int) p.getMinDataConstantAlarmAmount())
                        .hourDataExcessMode(ExcessMode.valueOf((int) p.getFkDitHourDataAlarmModeNo())).hourDataAlarmInterval((int) p.getHourAlarmInterval()).hourDataAlarmTimeMode(AlarmTimeMode.valueOf((int) p.getFkDitHourDataAlarmTimeModeNo()))
                        .hourZeroValueAlarmAmount((int) p.getHourDataZerosAlarmAmount()).hourSameValueAlarmAmount((int) p.getHourDataConstantAlarmAmount())
                        .dayDataExcessMode(ExcessMode.valueOf((int) p.getFkDitDayDataAlarmModeNo())).hourDataAlarmInterval((int) p.getHourAlarmInterval()).hourDataAlarmTimeMode(AlarmTimeMode.valueOf((int) p.getFkDitDayDataAlarmTimeModeNo()))
                        .stationPollExcessRoleId(p.getPkExcessId())
                        .build());
            }

            LimberAlarmCondition alarmCondition = LimberAlarmCondition.builder().conditions(conditionMap).build();
            alarmCondition.setExcessEnable(isExcessEnable);
            alarmCondition.setOverRangeEnable(isOverRangeEnable);
            clientProperty.setAlarmCondition(alarmCondition);
            return clientProperty;
        }catch (Exception e)
        {
            log.error("读取排放口污染因子数据失败："+e.getLocalizedMessage());
            return null;
        }
    }

    private void ReadLimberAlarmCondition(ClientProperty clientProperty,List<RtMonStationPollutant> pollutants) {
        try{

            Map<String, DataExcessCondition> conditionMap = new HashMap<>(pollutants.size());
            boolean isExcessEnable = false,isOverRangeEnable = false;
            for (RtMonStationPollutant p : pollutants) {
                boolean excessEnable = p.getOverStandardLower()!=null && p.getOverStandardUpper()!=null;
                isExcessEnable = isExcessEnable | excessEnable;
                isOverRangeEnable = isOverRangeEnable | p.isRangeAlarm();
                conditionMap.put(p.getFkPollutantCode(), DataExcessCondition.builder().dataType(InstrumentDataType.AI).compositionCode(p.getFkPollutantCode())
                        .rangeUpperLimit(p.getRangeUpper()).rangeLowerLimit(p.getRangeLower()).overRangeEnabled(p.isRangeAlarm())
                        .upperThreshold(p.getOverStandardUpper()).lowerThreshold(p.getOverStandardLower()).excessEnabled(excessEnable)
                        .realDataExcessMode(ExcessMode.valueOf((int) p.getFkDitRealDataAlarmModeNo())).realDataAlarmInterval((int) p.getRealAlarmInterval()).realDataAlarmTimeMode(AlarmTimeMode.valueOf((int) p.getFkDitRealDataAlarmTimeModeNo()))
                        .realZeroValueAlarmAmount((int) p.getRealDataZerosAlarmAmount()).realSameValueAlarmAmount((int) p.getRealDataConstantAlarmAmount())
                        .minDataExcessMode(ExcessMode.valueOf((int) p.getFkDitMinDataAlarmModeNo())).minDataAlarmInterval((int) p.getMinAlarmInterval()).minDataAlarmTimeMode(AlarmTimeMode.valueOf((int) p.getFkDitMinDataAlarmTimeModeNo()))
                        .minZeroValueAlarmAmount((int) p.getMinDataZerosAlarmAmount()).minSameValueAlarmAmount((int) p.getMinDataConstantAlarmAmount())
                        .hourDataExcessMode(ExcessMode.valueOf((int) p.getFkDitHourDataAlarmModeNo())).hourDataAlarmInterval((int) p.getHourAlarmInterval()).hourDataAlarmTimeMode(AlarmTimeMode.valueOf((int) p.getFkDitHourDataAlarmTimeModeNo()))
                        .hourZeroValueAlarmAmount((int) p.getHourDataZerosAlarmAmount()).hourSameValueAlarmAmount((int) p.getHourDataConstantAlarmAmount())
                        .dayDataExcessMode(ExcessMode.valueOf((int) p.getFkDitDayDataAlarmModeNo())).hourDataAlarmInterval((int) p.getHourAlarmInterval()).hourDataAlarmTimeMode(AlarmTimeMode.valueOf((int) p.getFkDitDayDataAlarmTimeModeNo()))
                        .stationPollExcessRoleId(p.getPkExcessId())
                        .build());
            }

            LimberAlarmCondition alarmCondition = LimberAlarmCondition.builder().conditions(conditionMap).build();
            alarmCondition.setExcessEnable(isExcessEnable);
            alarmCondition.setOverRangeEnable(isOverRangeEnable);
            clientProperty.setAlarmCondition(alarmCondition);
        }catch (Exception e)
        {
            log.error("读取排放口污染因子数据失败："+e.getLocalizedMessage());
        }
    }



}
